package tech.wangpai.microservice.product.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tech.wangpai.microservice.product.domain.Product;
import tech.wangpai.microservice.product.service.ProductService;

@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping(value = "/products")
    public ResponseEntity queryProductsByUsername(String pname) {
        return new ResponseEntity(productService.queryUserByUsername(pname), HttpStatus.OK);
    }

    @GetMapping(value = "/{product}")
    public ResponseEntity getProductById(@PathVariable("product") String productId) {
        return new ResponseEntity(productService.getProductById(productId), HttpStatus.OK);
    }

    @PostMapping(value = "/product")
    public ResponseEntity addProduct(@RequestBody Product product) {
        productService.addProduct(product);
        return new ResponseEntity(HttpStatus.OK);
    }

    @DeleteMapping(value = "/product/{productId}")
    public ResponseEntity delProductById(@PathVariable("productId") String productId) {
        productService.delProductById(productId);
        return new ResponseEntity(HttpStatus.OK);
    }

    @PutMapping(value = "/product")
    public ResponseEntity updateProductById(@RequestBody Product product) {
        productService.updateProductById(product);
        return new ResponseEntity(HttpStatus.OK);
    }
}
